////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   PrefixActionMethodDispatcher.java
// AUTHOR           :   ric.yik
// DATE CREATE      :   2006/4/27
// DESCRIPTION      :   
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/4/27      ric.yik         1.0                 source is created.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.common.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Dispatches a pageEvent to the corresponding processAction&lt;&lt;PageEvent&gt;&gt; method in an Action class. 
 * This classes eliminates the need of using a set of if-else statements on choosing the pageEvent 
 * process method in the action class.
 * 
 * <pre>
 * Sample usage:
 * 
 * class SampleAction extends SimpleMaintStyleAction {
 *     public ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn, 
 *             HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
 *         ActionForward actionForward = new ActionForward(mapping.getInput());
 *         MyModuleMaintenanceForm form = (MyModuleMaintenanceForm) formIn;
 *         if (this.isHandled(request)) {
 *             return actionForward;
 *         }
 *         PrefixActionMethodDispatcher dispatcher = new PrefixActionMethodDispatcher();
 *         dispatcher.setAction(this)
 *         dispatcher.setDefaultActionForward(actionForward);
 *         dispatcher.addPrefix(ProjectConstants.ACTION_SPEDIT); // ProjectConstants.ACTION_SPEDIT equals "SpEdit";
 *         dispatcher.processAction(form.getPageEvent(), mapping, request, response, errors);
 *         this.saveToken(request);
 *         this.setHandled(request);
 *         return actionForward;
 *     }
 * 
 *     public ActionForward processActionSpEdit(ActionMapping mapping, ActionForm formIn, 
 *             HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
 *         // ...
 *     }
 * }
 * 
 * If form.getPageEvent() equals "SpEditDescription", processActionSpEdit will be invoked.
 * </pre>
 * 
 * @author Ric Yik
 */
public class PrefixActionMethodDispatcher extends ActionMethodDispatcher {

    private final List prefixes = new ArrayList();

    public List getPrefixes() {
        return prefixes;
    }

    public PrefixActionMethodDispatcher addPrefix(String prefix) {
        this.getPrefixes().add(prefix);
        return this;
    }

    public PrefixActionMethodDispatcher addPrefix(Collection prefixes) {
        this.getPrefixes().addAll(prefixes);
        return this;
    }

    public ActionForward processAction(String pageEvent, ActionMapping mapping, ActionForm formIn, 
            HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
        for (Iterator prefix = this.getPrefixes().iterator(); prefix.hasNext();) {
            String p = (String) prefix.next();
            boolean match = (this.isProcessStatus()) ? this.getStatus().startsWith(p) : pageEvent.startsWith(p);
            if (match) {
                return super.processAction(p, mapping, formIn, request, response, errors);
            }
        }
        return this.processWithNextDispatcher(pageEvent, mapping, formIn, request, response, errors);
    }

}
